package com.cpi.cornpp.futurepurchase.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.cpi.cornpp.futurepurchase.form.FuturePurchaseForm;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseByCustomerView;
import com.cpi.cornpp.product.view.ProductView;
import com.cs.common.utils.NumberUtil;

/**
 * Customer Future Purchase 
 * Add Product Action
 * 
 * @author Edgar Joao
 *
 */
public class CustomerFuturePurchaseAddProductAction extends MappingDispatchAction{
	/** Identifier default workflow*/
	private final static String DEFAULT = "default";//$NON-NLS-1$
	/** Identifier session atribute for list of customer purchase*/
	private final static String LIST_CUSTOMER_PURCHASE_PRODUCTS = "LIST_CUSTOMER_PURCHASE_PRODUCTS";//$NON-NLS-1$
	
	private final static String CONTRACT_TYPE_LABEL = "contractType";//$NON-NLS-1$
	private final static String CONTRACT_NUMBERS_LABEL = "contratNumbers";//$NON-NLS-1$
	private final static String ACCOUNT_ID_LABEL = "accountId";//$NON-NLS-1$
	private final static String APPLICATION_ID_LABEL = "applicationId";//$NON-NLS-1$
	private final static String PRICE_LABEL = "price";//$NON-NLS-1$
	private final static String PURCHASE_DATE_LABEL = "purchaseDate";//$NON-NLS-1$
	private final static String REFERENCE_LABEL = "reference";//$NON-NLS-1$
	
	private final static String FUTURE_IN_SESSION_LABEL = "FuturePurchaseByCustomerView";//$NON-NLS-1$
	
	private void saveFutureInSession(HttpServletRequest request){
		HttpSession httpSession = null;
		FuturePurchaseByCustomerView futurePurchaseByCustomerView = null;
		if( request.getParameter(ACCOUNT_ID_LABEL) != null ){
			httpSession = request.getSession();
			futurePurchaseByCustomerView = new FuturePurchaseByCustomerView();
			futurePurchaseByCustomerView.setAccountPeriod(NumberUtil.getInt(request.getParameter(ACCOUNT_ID_LABEL)));
			futurePurchaseByCustomerView.setApplicationPeriod(NumberUtil.getInt(request.getParameter(APPLICATION_ID_LABEL)));
			futurePurchaseByCustomerView.setDate(request.getParameter(PURCHASE_DATE_LABEL));
			futurePurchaseByCustomerView.setDealNumber(request.getParameter(CONTRACT_NUMBERS_LABEL));
			futurePurchaseByCustomerView.setDealType(request.getParameter(CONTRACT_TYPE_LABEL));
			futurePurchaseByCustomerView.setPrice(request.getParameter(PRICE_LABEL));
			futurePurchaseByCustomerView.setReference(request.getParameter(REFERENCE_LABEL));
			
			httpSession.setAttribute(FUTURE_IN_SESSION_LABEL, futurePurchaseByCustomerView);
		}
	}
	/**
	 * Metodo que redirecciona a la alta de producto
	 * @author Edgar Joao
	 */
	public ActionForward goProductAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {		
		FuturePurchaseForm futurePurchaseForm = null;
		futurePurchaseForm = (FuturePurchaseForm) form;
		
		this.saveFutureInSession(request);
		
		ProductView productView = null;
		productView = new ProductView();
		productView.setDescription(StringUtils.EMPTY);
		productView.setProductId(StringUtils.EMPTY);
		productView.setTon(StringUtils.EMPTY);
		
		futurePurchaseForm.setProductViewSearch(productView);
		futurePurchaseForm.reset(mapping, request);		
		return mapping.findForward(DEFAULT);
	}	
	/**
	 * 
	 * Metodo que agrega elementos a la lista en sesion
	 * 
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward customerFutureAddProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		
		HttpSession httpSession = null;				
		httpSession = request.getSession(false);
		FuturePurchaseForm futurePurchaseForm = null;
		futurePurchaseForm = (FuturePurchaseForm) form;
		ProductView productView = null;
		productView = new ProductView();
	
		List listInSession = (List) httpSession.getAttribute(LIST_CUSTOMER_PURCHASE_PRODUCTS);
		productView.setProductId(futurePurchaseForm.getProductViewSearch().getProductId());
		productView.setDescription(futurePurchaseForm.getProductViewSearch().getDescription());
		productView.setTon(futurePurchaseForm.getProductViewSearch().getTon());		
		listInSession.add(productView);
		httpSession.setAttribute(LIST_CUSTOMER_PURCHASE_PRODUCTS, listInSession);
		
		return mapping.findForward(DEFAULT);
	}		
	/**
	 * 
	 * Metodo que elimina un registro de la lista en sesion
	 * 
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward customerFutureDeleteProduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HttpSession httpSession = null;				
		httpSession = request.getSession(false);
		String parameterId = null;
		List list = null;
		this.saveFutureInSession(request);
		parameterId = request.getParameter("productId");
		list = (List) httpSession.getAttribute(LIST_CUSTOMER_PURCHASE_PRODUCTS);
		
		for (int i = 0; i < list.size(); i++) {
			ProductView view = (ProductView) list.get(i);
			if(StringUtils.equals(view.getProductId().trim(), parameterId.trim())){
				list.remove(i);
				httpSession.setAttribute(LIST_CUSTOMER_PURCHASE_PRODUCTS, list);
				break;
			}
		}
		
		return mapping.findForward(DEFAULT);
	}
	
	
}
